package com.corpgovernment.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.corpgovernment.common.apollo.CommonApollo;
import com.corpgovernment.common.utils.req.AbRequest;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Component
public class ABTestUtil {
    //

    @Autowired
    private CommonApollo commonApollo;

    private static final String AB_TEST_ = "ab_test_";

    /**
     * 黑白名单 +随机比例 +相同场景下同一个key输出结果一致性
     *
     * @Return "A"-实验组   "B"-对照组（默认）
     */
    public String getAbTestResult(AbRequest abRequest){
        String key = abRequest.getKey();
        // 定义实验组和对照组
        String[] groups = {"A", "B"};
        // 随机分组（如果用户在白名单中，则强制分配到实验组A；如果用户在黑名单中，则强制分配到对照组B）
        String group = "B";
        if(ObjectUtil.isNull(abRequest.getKey())){
            return group;
        }
        if(ObjectUtil.isNull(abRequest.getScen())){
            return group;
        }
        if (CollectionUtil.isNotEmpty(abRequest.getWhitelist()) && abRequest.getWhitelist().contains(key)) {
            group = "A";
        } else if (CollectionUtil.isNotEmpty(abRequest.getBlacklist()) && abRequest.getBlacklist().contains(key)) {
            group = "B";
        } else {
            long keyHashCode = Math.abs(key.hashCode());
            long scenHashCode = Math.abs(abRequest.getScen().hashCode());
            Random random = new Random(keyHashCode+scenHashCode);
            double value = random.nextDouble();
            if (value < abRequest.getRatio()) {
                group = "A";
            } else {
                group = "B";
            }
        }
        
        return group;
    }
    /**
     * @Return true-实验组   false-对照组（默认）
     */
    public Boolean getBooleanAbTestResult(AbRequest abRequest){
        return "A".equals(this.getAbTestResult(abRequest));
    }
    /**
     * @Return true-实验组   false-对照组（默认）
     */
    public Boolean getBooleanAbTestResultWithAuto(String scen,String key){
        return "A".equals(this.getAbTestResultWithAuto(scen,key));
    }
    /**
     * 黑白名单 +随机比例 +相同场景下同一个key输出结果一致性
     *
     * @Return "A"-实验组   "B"-对照组（默认）
     */
    public String getAbTestResultWithAuto(String scen,String key){
        String json = commonApollo.getAppProperty(AB_TEST_+ scen,null);
        if(ObjectUtil.isNull(json)){
            return "B";
        }
        AbRequest request = JsonUtils.parse(json,AbRequest.class);
        request.setKey(key);
        return this.getAbTestResult(request);
    }


    public static void main(String[] args) {

        AbRequest abRequest = new AbRequest();
        abRequest.setKey("1111");
        abRequest.setScen("stock");
        abRequest.setRatio(0.2d);
        //String a = getAbTestResult(abRequest);

        abRequest.setBlacklist(Arrays.asList(abRequest.getKey()));
        abRequest.setWhitelist(Arrays.asList(abRequest.getKey()));
        //abRequest.setScen("2222");
        //String b = getAbTestResult(abRequest);
        System.out.println(1);
    }

}